package com.uniledger.exchange.mine.scheme;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.uniledger.commonlib.util.DisplayUtil;
import com.uniledger.exchange.HomeActivity;
import com.uniledger.exchange.R;
import com.uniledger.exchange.base.activity.BaseActivity;
import com.uniledger.exchange.home.scheme.TopUpActivity;
import com.uniledger.exchange.model.HttpResult;
import com.uniledger.exchange.model.StellarList;
import com.uniledger.exchange.model.api.Api;
import com.uniledger.exchange.model.rx.RxException;
import com.uniledger.exchange.model.rx.RxProgress;
import com.uniledger.exchange.model.rx.RxSchedulers;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import butterknife.BindString;
import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * 交易记录
 */
public class HistoryActivity extends BaseActivity {

    @BindView(R.id.recycler_history)
    RecyclerView recyclerHistory;

    HistoryAdapter historyAdapter;

    @BindString(R.string.bill_bar_title)
    String title;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, HistoryActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        LinearLayoutManager manager = new LinearLayoutManager(this);

        HorizontalDividerItemDecoration decoration = new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.TRANSPARENT)
                .size(DisplayUtil.dip2px(this,16))
                .showLastDivider()
                .build();

        recyclerHistory.addItemDecoration(decoration);

        recyclerHistory.setLayoutManager(manager);
        historyAdapter = new HistoryAdapter(this);
        recyclerHistory.setAdapter(historyAdapter);

        Api.getService().getTrxHistory(1, 200)
                .compose(RxSchedulers.io_main())
                .compose(RxProgress.applyProgressBar(this))
                .subscribe(new Consumer<HttpResult<StellarList>>() {
                    @Override
                    public void accept(HttpResult<StellarList> httpResult) throws Exception {
                        historyAdapter.addData(httpResult.data.transactions);
                    }
                }, new RxException<>(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }));
    }


    @Override
    public void getParams() {

    }

    @Override
    public void initBar() {
        super.initBar();
        toolbar.setTitle(title);
    }

    @Override
    public void onBackPressed() {
        HomeActivity.start(HistoryActivity.this);
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
